##### ################################################### ######
#####                                                     ######
#####   Input: clean experiment data                      ######
#####   Output: analysis of main outcomes                 ######
#####                                                     ######
##### ################################################### ######

setwd("/Users/lotte/Dropbox/PhD/style_experiment/replication")
rm(list=ls())

# Load libraries

library(data.table) # CRAN v1.14.2
library(plyr) # CRAN v1.8.6
library(dplyr) # CRAN v1.0.9
library(tidyverse) # CRAN v1.3.1
library(ggplot2) # CRAN v3.3.6
library(broom) # CRAN v0.7.11
library(patchwork) # CRAN v1.1.1
library(estimatr) # CRAN v0.30.6
library(margins) # CRAN v0.3.26

# Load clean experiment data

load("data/style_data.Rdata")
load("data/emotion_data.Rdata")
load("data/aggression_data.Rdata")
load("data/evidence_data.Rdata")

emotion_likeability_woman <- lm(perceived_likeability ~ objective_style_emotion -1,
                                            data = emotion,
                                     subset = emotion$mp_gender=="Woman")
emotion_likeability_man <- lm(perceived_likeability ~ objective_style_emotion -1,
                                data = emotion,
                                subset = emotion$mp_gender=="Man")
aggression_likeability_woman <- lm(perceived_likeability ~ objective_style_aggression -1 ,
                                data = aggression,
                                subset = aggression$mp_gender=="Woman")
aggression_likeability_man <- lm(perceived_likeability ~ objective_style_aggression -1,
                                   data = aggression,
                                   subset = aggression$mp_gender=="Man")
evidence_likeability_woman <- lm(perceived_likeability ~ prevalence_evidence -1,
                                   data = evidence,
                                   subset = evidence$mp_gender=="Woman")
evidence_likeability_man <- lm(perceived_likeability ~ prevalence_evidence -1,
                                 data = evidence,
                                 subset = evidence$mp_gender=="Man")

likeability <- bind_rows(
  tidy(emotion_likeability_woman, conf.int = T)%>%mutate(model="Emotion", gender="Woman", outcome="Likeability"),
  tidy(emotion_likeability_man, conf.int = T)%>%mutate(model="Emotion", gender="Man", outcome="Likeability"),
  tidy(aggression_likeability_woman, conf.int = T)%>%mutate(model="Aggression", gender="Woman", outcome="Likeability"),
  tidy(aggression_likeability_man, conf.int = T)%>%mutate(model="Aggression", gender="Man", outcome="Likeability"),
  tidy(evidence_likeability_woman, conf.int = T)%>%mutate(model="Evidence", gender="Woman", outcome="Likeability"),
  tidy(evidence_likeability_man, conf.int = T)%>%mutate(model="Evidence", gender="Man", outcome="Likeability"))%>%
    mutate(treatment=c("Non-emotional", "Emotional", "Non-emotional", "Emotional", "Non-aggressive", "Aggressive",
                       "Non-aggressive", "Aggressive", "Statistical", "Anecdotal", "Statistical", "Anecdotal"))

likeability_plot <- ggplot(likeability, aes(y=estimate,x=treatment, group = gender, color = gender)) +
  geom_point(position = position_dodge(width=0.2)) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.2), aes(ymin = conf.low, ymax = conf.high, linetype = gender)) +
  facet_grid(outcome~model,scales="free") +
  theme_bw() + ylim(2.75,4) +
  scale_color_manual(values = c("lightseagreen", "grey40")) +
  scale_shape_manual(values=c(19,1)) +
  labs(color="MP gender", linetype="MP gender", shape="MP gender") +
  theme(axis.text=element_text(size=11),
        strip.text.y = element_text(size=11),
        strip.text.x = element_text(size=11),
        legend.title = element_text(size=11))

emotion_competence_woman <- lm(perceived_competence ~ objective_style_emotion -1,
                                data = emotion,
                                subset = emotion$mp_gender=="Woman")
emotion_competence_man <- lm(perceived_competence ~ objective_style_emotion -1,
                              data = emotion,
                              subset = emotion$mp_gender=="Man")
aggression_competence_woman <- lm(perceived_competence ~ objective_style_aggression -1,
                                   data = aggression,
                                   subset = aggression$mp_gender=="Woman")
aggression_competence_man <- lm(perceived_competence ~ objective_style_aggression -1,
                                 data = aggression,
                                 subset = aggression$mp_gender=="Man")
evidence_competence_woman <- lm(perceived_competence ~ prevalence_evidence -1,
                                 data = evidence,
                                 subset = evidence$mp_gender=="Woman")
evidence_competence_man <- lm(perceived_competence ~ prevalence_evidence -1,
                               data = evidence,
                               subset = evidence$mp_gender=="Man")
competence <- bind_rows(
  tidy(emotion_competence_woman, conf.int = T)%>%mutate(model="Emotion", gender="Woman", outcome="Competence"),
  tidy(emotion_competence_man, conf.int = T)%>%mutate(model="Emotion", gender="Man", outcome="Competence"),
  tidy(aggression_competence_woman, conf.int = T)%>%mutate(model="Aggression", gender="Woman", outcome="Competence"),
  tidy(aggression_competence_man, conf.int = T)%>%mutate(model="Aggression", gender="Man", outcome="Competence"),
  tidy(evidence_competence_woman, conf.int = T)%>%mutate(model="Evidence", gender="Woman", outcome="Competence"),
  tidy(evidence_competence_man, conf.int = T)%>%mutate(model="Evidence", gender="Man", outcome="Competence"))%>%
  mutate(treatment=c("Non-emotional", "Emotional", "Non-emotional", "Emotional", "Non-aggressive", "Aggressive",
                     "Non-aggressive", "Aggressive", "Statistical", "Anecdotal", "Statistical", "Anecdotal"))

competence_plot <- ggplot(competence, aes(y=estimate,x=treatment, group = gender, color = gender)) +
  geom_point(position = position_dodge(width=0.2)) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.2), aes(ymin = conf.low, ymax = conf.high, linetype = gender)) +
  facet_grid(outcome~model,scales="free") +
  theme_bw() + ylim(2.75,4) +
  scale_color_manual(values = c("lightseagreen", "grey40")) +
  scale_shape_manual(values=c(19,1)) +
  labs(color="MP gender", linetype="MP gender", shape="MP gender") +
  theme(axis.text=element_text(size=11),
        strip.text.y = element_text(size=11),
        strip.text.x = element_text(size=11),
        legend.title = element_text(size=11))

emotion_perception_woman <- lm(perceived_emotion ~ objective_style_emotion -1,
                               data = emotion,
                               subset = emotion$mp_gender=="Woman")

emotion_perception_man <- lm(perceived_emotion ~ objective_style_emotion -1,
                             data = emotion,
                             subset = emotion$mp_gender=="Man")

aggression_perception_woman <- lm(perceived_aggression ~ objective_style_aggression -1,
                                  data = aggression,
                                  subset = aggression$mp_gender=="Woman")
aggression_perception_man <- lm(perceived_aggression ~ objective_style_aggression -1,
                                data = aggression,
                                subset = aggression$mp_gender=="Man")
evidence_perception_woman <- lm(perceived_evidence ~ prevalence_evidence -1,
                                data = evidence,
                                subset = evidence$mp_gender=="Woman")
evidence_perception_man <- lm(perceived_evidence ~ prevalence_evidence -1,
                              data = evidence,
                              subset = evidence$mp_gender=="Man")

style_perception <- bind_rows(
  tidy(emotion_perception_woman, conf.int = T)%>%mutate(model="Emotion", gender="Woman", outcome="Style Perception"),
  tidy(emotion_perception_man, conf.int = T)%>%mutate(model="Emotion", gender="Man", outcome="Style Perception"),
  tidy(aggression_perception_woman, conf.int = T)%>%mutate(model="Aggression", gender="Woman", outcome="Style Perception"),
  tidy(aggression_perception_man, conf.int = T)%>%mutate(model="Aggression", gender="Man", outcome="Style Perception"),
  tidy(evidence_perception_woman, conf.int = T)%>%mutate(model="Evidence", gender="Woman", outcome="Style Perception"),
  tidy(evidence_perception_man, conf.int = T)%>%mutate(model="Evidence", gender="Man", outcome="Style Perception"))%>%
  mutate(treatment=c("Non-emotional", "Emotional", "Non-emotional", "Emotional", "Non-aggressive", "Aggressive",
                     "Non-aggressive", "Aggressive", "Statistical", "Anecdotal", "Statistical", "Anecdotal"))

style_perception_plot <- ggplot(style_perception, aes(y=estimate,x=treatment, group = gender, color = gender)) +
  geom_point(position = position_dodge(width=0.2)) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.2), aes(ymin = conf.low, ymax = conf.high, linetype = gender)) +
  facet_grid(outcome~model,scales="free") +
  theme_bw() + ylim(2.75,4) +
  scale_color_manual(values = c("lightseagreen", "grey40")) +
  scale_shape_manual(values=c(19,1)) +
  labs(color="MP gender", linetype="MP gender", shape="MP gender") +
  theme(axis.text=element_text(size=11),
        strip.text.y = element_text(size=11),
        strip.text.x = element_text(size=11),
        legend.title = element_text(size=11))

plots_revisions <- likeability_plot / competence_plot / style_perception_plot
ggsave("analysis/plots/figure_3_mp_gender_conditional.pdf", plots_revisions, width = 10, height = 6.5)

## Pooled model

pooled_likeability_clustered <- lm_robust(perceived_likeability ~ mp_gender*style_categorical + policy,
                         clusters = style_data$ID, se_type = "stata",
                         data = style_data)

pooled_likeability <- lm(perceived_likeability ~ mp_gender*style_categorical + policy,
                         data = style_data)

pooled_competence_clustered <- lm_robust(perceived_competence ~ mp_gender*style_categorical + policy,
                                clusters = ID, se_type = "stata",
                                data = style_data)

pooled_competence <- lm(perceived_competence ~ mp_gender*style_categorical + policy,
                        data = style_data)

my_clustered_errors <- list(pooled_likeability$std.error, pooled_competence$std.error)

save(pooled_likeability_clustered, pooled_likeability, pooled_competence_clustered, pooled_competence, my_clustered_errors,
     file = "analysis/tables/conditional_pooled.Rdata")

## Policy areas

likeability_policy <- lm(perceived_likeability ~ mp_gender*policy + style, data = style_data)
likeability_policy_clustered <- lm_robust(perceived_likeability ~ mp_gender*policy + style, data = style_data,
                                          clusters = style_data$ID, se_type = "stata")
competence_policy <- lm(perceived_competence ~ mp_gender*policy + style, data = style_data)
competence_policy_clustered <- lm_robust(perceived_competence~ mp_gender*policy + style, data = style_data,
                                          clusters = style_data$ID, se_type = "stata")
my_clustered_errors <- list(likeability_policy_clustered$std.error, competence_policy_clustered$std.error)

save(likeability_policy, competence_policy, likeability_policy_clustered, competence_policy_clustered,
     my_clustered_errors, file = "analysis/tables/policy.Rdata")

